package com.lwphk.easydocs.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ClassReaderUtil {

	
	
	 public static List<Field> getAllFields(Class<?> clazz) {
	        List<Field> fields = new ArrayList<>();
	        Class<?> current = clazz;
	        while (current != null) {
	            fields.addAll(Arrays.asList(current.getDeclaredFields()));
	            current = current.getSuperclass();
	        }
	        return fields;
	        
	}
	
	 /**
	  * 
	  * @param clazz
	  * @param filterStatic  排除静态
	  * @param filterFinal   排除final
	  * @return
	  */
	public static List<Field> getAllFields(Class<?> clazz,boolean filterStatic,boolean filterFinal) {
	        List<Field> fields = getAllFields(clazz);
	        if(filterStatic) {
	        	fields = fields.stream().filter(e->!Modifier.isStatic(e.getModifiers())).collect(Collectors.toList());
	        }
	        if(filterFinal) {
	        	fields = fields.stream().filter(e->!Modifier.isFinal(e.getModifiers())).collect(Collectors.toList());
	        }
	        return fields;
	}
	
	
	public static List<Method> getAllMethods(Class<?> clazz) {
        List<Method> methods = new ArrayList<>();
        Class<?> current = clazz;
        while (current != null) {
            methods.addAll(Arrays.asList(current.getDeclaredMethods()));
            current = current.getSuperclass();
        }
        return methods;
    }
	
}
